package com.preskool.controller;

import com.preskool.dto.ActionResult;
import com.preskool.po.Transport;
import com.preskool.serevice.TransportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:薛亮亮
 * @Date 2021/1/7 15:58
 * @Discription:校园大巴控制层
 */
@RestController
@Slf4j
@Api(value = "Transport接口文档")
public class TransportContrller {
    @Autowired
    private TransportService transportService;

    /**
     * 查询所有的校园交通情况
     * @param currentPage 起始页数
     * @param pageSize 每页显示条数
     * @param sortName 排序的字段
     * @return 返回查询的交通集合
     */
    @RequestMapping(value = "/transport", method = RequestMethod.POST)
    @ApiOperation(value = "查询所有的交通信息路线")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功"),
            @ApiResponse(code = 201,message = "请求失败")
    })
    public ActionResult findTransports(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "3") Integer pageSize,  String sortName) {
        //打印日志
        log.info("currentPage"+currentPage);
        log.info("pageSize"+pageSize);
        List<Transport> transports = transportService.findTransports(currentPage, pageSize, sortName);
        log.info("transports" + transports);
        return new ActionResult(200, "", transports);
    }

    /**
     * 添加校园交通
     * @param transport 校园交通数据
     * @return 返回添加结果
     */
    @RequestMapping(value = "/addTransport",method = RequestMethod.PUT)
    @ApiOperation(value = "添加交通路线功能")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功"),
            @ApiResponse(code = 201,message = "请求失败")
    })
    public ActionResult insertTransport(@RequestBody Transport transport){
        int i = transportService.addTransport(transport);
        if (i>0){
            return new ActionResult(200,"insert successful",null);
        }
            return new ActionResult(201,"insert failed",null);
    }


    /**
     * 删除校园交通路线
     * @param routeName 交通路线
     * @return
     */
    @RequestMapping(value = "/deleteTransport",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除交通路线功能")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功"),
            @ApiResponse(code = 201,message = "请求失败")
    })
    public ActionResult deleteTransport(String routeName){
        int i = transportService.deleteTransport(routeName);
        if (i>0){
            return new ActionResult(200,"delete successful",null);
        }
        return new ActionResult(201,"delete failed",null);
    }

    /**
     * 更新校园交通路线
     * @param transport
     * @return
     */
    @RequestMapping(value = "updateTransport",method = RequestMethod.POST)
    @ApiOperation(value = "更新交通路线信息")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功"),
            @ApiResponse(code = 201,message = "请求失败")
    })
    public ActionResult updateTransport(@RequestBody Transport transport){
        int i = transportService.updateTransport(transport);
        if (i>0){
            return new ActionResult(200,"update successful",null);
        }
        log.info("transport:"+transport);
        return new ActionResult(201,"update failed",null);
    }

}